package view;

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JLabel;
import java.awt.Point;
import java.awt.Font;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;

import model.HistoryModel;
import model.userModel;

import com.toedter.calendar.JDateChooser;
import common.loadData;

import dao.HistoryDao;
import dao.encrypt;
import dao.userdao;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class addHistory extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel panelTitle = null;
	private JLabel lbtitle = null;
	private JLabel LabelDate = null;
	private JLabel LabelPlace = null;
	private JTextField txtpalce = null;
	private JPanel PanelDetail = null;
	private JTextArea txtDetail = null;
	private JButton btnSave = null;
	private JButton btnExit = null;
	private JButton btnCancel = null;
	private JDateChooser txtDate;
	private JLabel lbName = null;
	private JTextField txtName = null;

	public addHistory() {
		super();
		initialize();
		loadDataByID();
	}

	private void initialize() {
		this.setSize(605, 523);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/image/List.png")));
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("Add History");
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lbName = new JLabel();
			lbName.setBounds(new Rectangle(45, 75, 54, 28));
			lbName.setText("Name:");
			LabelPlace = new JLabel();
			LabelPlace.setBounds(new Rectangle(46, 120, 52, 30));
			LabelPlace.setText("Place:");
			LabelDate = new JLabel();
			LabelDate.setBounds(new Rectangle(311, 121, 52, 30));
			LabelDate.setText("Date:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getPanelTitle(), null);
			jContentPane.add(LabelDate, null);
			jContentPane.add(LabelPlace, null);
			jContentPane.add(getTxtpalce(), null);
			jContentPane.add(getPanelDetail(), null);
			jContentPane.add(getBtnSave(), null);
			jContentPane.add(getBtnExit(), null);
			jContentPane.add(getTxtDate(), null);
			jContentPane.add(lbName, null);
			jContentPane.add(getTxtName(), null);
		}
		return jContentPane;
	}

	private JPanel getPanelTitle() {
		if (panelTitle == null) {
			lbtitle = new JLabel();
			lbtitle.setLocation(new Point(0, 0));
			lbtitle.setFont(new Font("Dialog", Font.BOLD, 18));
			lbtitle.setForeground(new Color(46, 130, 124));
			lbtitle.setText("Add History");
			lbtitle.setSize(new Dimension(106, 27));
			panelTitle = new JPanel();
			panelTitle.setLayout(null);
			panelTitle.setBounds(new Rectangle(196, 29, 107, 31));
			panelTitle.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.red), "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			panelTitle.add(lbtitle, null);
			panelTitle.add(getBtnCancel(), null);
		}
		return panelTitle;
	}

	private JTextField getTxtpalce() {
		if (txtpalce == null) {
			txtpalce = new JTextField();
			txtpalce.setLocation(new Point(106, 121));
			txtpalce.setSize(new Dimension(166, 27));
		}
		return txtpalce;
	}

	private JPanel getPanelDetail() {
		if (PanelDetail == null) {
			PanelDetail = new JPanel();
			PanelDetail.setLayout(null);
			PanelDetail.setBounds(new Rectangle(44, 166, 512, 255));
			PanelDetail.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(51, 51, 255), 1), "Detail", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 255)));
			PanelDetail.add(getTxtDetail(), null);
		}
		return PanelDetail;
	}

	private JTextArea getTxtDetail() {
		if (txtDetail == null) {
			txtDetail = new JTextArea();
			txtDetail.setBounds(new Rectangle(17, 25, 477, 215));
		}
		return txtDetail;
	}

	private JButton getBtnSave() {
		if (btnSave == null) {
			btnSave = new JButton();
			btnSave.setBounds(new Rectangle(360, 450, 90, 30));
			btnSave.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnSave.setIcon(new ImageIcon(getClass().getResource("/image/Save.png")));
			btnSave.setText("Save");
			btnSave.setHorizontalAlignment(SwingConstants.LEFT);
			btnSave.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// validate
				    	 HistoryModel model = new HistoryModel();
							if(panelHistiry.IdHis>=0)
							{
								model.setId(panelHistiry.IdHis);
							}
							model.setName(txtName.getText());
							SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
							String da = sdf.format(txtDate.getDate());
							model.setDate(da);
							model.setDetail(txtDetail.getText());
							model.setPlace(txtpalce.getText());
							java.util.Date now = new java.util.Date();
							String d = sdf.format(now);
							model.setLastUpdated(d);
							// process add User
							if(panelHistiry.IdHis>=0)
							{
								int rs = HistoryDao.updateHistory(model);
								if(rs==1)
								{
									JOptionPane.showMessageDialog(null, "Update sucessful!");
									loadData.loadDataTablehis();
									if(login.idrole==0)
										loadData.tab=8;
									else
										loadData.tab=7;
								}
								else
								{
									JOptionPane.showMessageDialog(null, "Update failed!");
								}
							}
							else
								{
									int rs = HistoryDao.insertHistory(model);
									if(rs==1)
									{
										JOptionPane.showMessageDialog(null, "Insert sucessful!");
										loadData.loadDataTablehis();
										if(login.idrole==0)
											loadData.tab=8;
										else
											loadData.tab=7;
					
									}
									else
									{
										JOptionPane.showMessageDialog(null, "Insert failed!");
									}
								}
								mainForm a = new mainForm();
								a.setVisible(true);
								dispose();
					}	
			});
		}
		return btnSave;
	}

	private JButton getBtnExit() {
		if (btnExit == null) {
			btnExit = new JButton();
			btnExit.setBounds(new Rectangle(471, 450, 85, 31));
			btnExit.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnExit.setIcon(new ImageIcon(getClass().getResource("/image/Close.png")));
			btnExit.setText("Exit");
			btnExit.setHorizontalAlignment(SwingConstants.LEFT);
			btnExit.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if(login.idrole==0)
						loadData.tab=8;
					else
						loadData.tab=7;
					mainForm a = new mainForm();
					a.setVisible(true);
					dispose();
					
				}
			});
		}
		return btnExit;
	}

	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setBounds(new Rectangle(5, 32, 83, 34));
			btnCancel.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnCancel.setIcon(new ImageIcon(getClass().getResource("/image/Close.png")));
			btnCancel.setText("Exit");
			btnCancel.setHorizontalAlignment(SwingConstants.LEFT);
		}
		return btnCancel;
	}

	private JDateChooser getTxtDate() {
		if (txtDate == null) {
			txtDate = new JDateChooser();
			txtDate.setDateFormatString("MM/dd/yyyy");
			txtDate.setLocation(new Point(373, 123));
			txtDate.setSize(new Dimension(181, 27));
			txtDate.getDateEditor().setEnabled(false);
		}
		return txtDate;
	}

	private JTextField getTxtName() {
		if (txtName == null) {
			txtName = new JTextField();
			txtName.setLocation(new Point(105, 77));
			txtName.setSize(new Dimension(166, 27));
		}
		return txtName;
	}
	
	private void loadDataByID()
	{
		if(panelHistiry.IdHis>=0)
		{
		lbtitle.setText("Update History");
		lbtitle.setSize(new Dimension(140, 28));
		panelTitle.setSize(new Dimension(140, 28));
		btnSave.setIcon(new ImageIcon(getClass().getResource("/image/Save.png")));
		btnSave.setText("Save");
		btnSave.setSize(new Dimension(90, 28));
		ArrayList<HistoryModel> user = HistoryDao.getbyID(panelHistiry.IdHis);
		for(HistoryModel model : user)
		{
			txtName.setText(model.getName());
			try{
				SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
				txtDate.setDate(sdf.parse(model.getDate()));
				}catch (Exception e) {
					e.printStackTrace();
				}
			txtDetail.setText(model.getDetail());
			txtpalce.setText(model.getPlace());	
		}
	}
	}


}
